﻿var OrgHelper = {
    OrgList: [],
    rootId: '',


    GetOrgByName: function (orgName) {
        var me = this;
        var result = new Array();
        $.each(SqlHelper.OrgList, function (i, val) {
            if (val.NAME.indexOf(orgName) != -1)
                result[result.length] = val;
        });
        return result;
    },
    //ztree配置
    Setting: {
        view: {
            dblClickExpand: false,
            showLine: true,
            selectedMulti: false
        },
        data: {
            simpleData: {
                enable: true,
                idKey: "id",
                pIdKey: "parentId",
                rootPId: null
            },
            key: {
                name: "orgName"
            }
        },
        callback: {
            onDblClick: zTreeOnDblClick
        }
    },

    //ztree配置
    Setting1: {
        view: {
            dblClickExpand: false,
            showLine: true,
            selectedMulti: false
        },
        data: {
            simpleData: {
                enable: true,
                idKey: "id",
                pIdKey: "parentId",
                rootPId: null
            },
            key: {
                name: "orgName"
            }
        },
        callback: {
            onClick: SelectNode
        }
    },

    BindOrg: function (source) {
        zTreeNodes = source;
        zTreeObj = $.fn.zTree.init($("#treeOrg"), OrgHelper.Setting, zTreeNodes);
        zTreeObj.expandAll(true);
    },

    BindOrgForOpen: function (source) {
        zTreeNodes = source;
        zTreeObj = $.fn.zTree.init($("#ulOpenTempOrg"), OrgHelper.Setting1, zTreeNodes);
        zTreeObj.expandAll(true);
    },

    SearchNode: function (nodeName) {
        var me = this;
        if (nodeName == this.SearchInfo.Content) {
            ++me.SearchInfo.Position;
        }
        else this.SearchInfo = {
            Position: 0,
            Content: nodeName
        }
        if (nodeName != undefined && nodeName.length > 0) {
            var nodes = zTreeObj.transformToArray(zTreeObj.getNodes());

            var tmp = -1;
            var firstSameNode = undefined;
            var node = undefined;
            $.each(nodes, function (i, val) {
                var name = val.name != undefined ? val.name : val.NAME;
                if (name.indexOf(nodeName) != -1) {
                    ++tmp;
                    if (tmp == 0)
                        firstSameNode = val;
                    if (tmp == me.SearchInfo.Position) {
                        node = val;
                        return false;
                    }

                }

            })

            if (node != undefined) {
                zTreeObj.selectNode(node, false);
            }
            else {
                if (firstSameNode != undefined)
                    zTreeObj.selectNode(firstSameNode, false);
                me.SearchInfo.Position = 0;
            }
        }

    },

    SearchInfo: {
        Position: 0,
        Content: ''
    }


}
function SelectNode(event, treeId, treeNode) {

    SqlHelper.GetAllPackage(BindPackage, treeNode.ID);


};

//绑定包
/*用于1、打开流程时的包列表

 */
function BindPackage(data) {
    $("#selectOpenTemPackage").html("");

    $("#selectOpenTemPackage").unbind();
    $("#selectOpenTemPackage").change(function () {
        var packageId = $("#selectOpenTemPackage").find('option:selected').val();
        BindTemplate(packageId);
    })
    $.each(data, function (i, val) {
        var option = $("<option  value='" + val.id + "' >" + val.packageName + "</option>");
        $("#selectOpenTemPackage").append(option);
    });
    if ($("#selectOpenTemPackage").find("option").length > 0) {
        $("#selectOpenTemPackage").select(0);
        var packageId = $("#selectOpenTemPackage").find('option').val();
        BindTemplate(packageId)
    }
    else {
        $("#selectTemplate").html("");
    }
}

//根据选择的包，绑定模版
/*用于打开流程窗口*/
function BindTemplate(packageId) {
    var container = $("#selectTemplate");
    container.empty();
    var versionId;
    $.each(SqlHelper.TemplateList, function (i, val) {
        /*
         TemplateList按照VERSION_ID，VERSION两个字段排序
         遍历模版时，当VERSION_ID与临时存储的versionId相同，说明是同一个模版的不同版本，继续向下寻找，
         否则说明不是同一模版，需向列表中添加新项
         */
        $("#ulVersion").empty();
        if (val.packageId == packageId && (versionId == undefined || versionId != val.versionId)) {
            //更改临时存储的versionId
            versionId = val.versionId;
            //向列表中添加新项
            var li = $("<li id='" + val.id + "'  title='" + val.templateName + "'>" + val.templateName + "</li>");
            container.append(li);

            li.bind({
                //单击事件，点击后加载版本列表
                "click": function () {
                    openTemplateId = li[0].id;
                    container.find("li").css("background-color", "white");
                    li.css("background-color", "#26a0da");
                    $("#ulVersion").empty();


                    $.each(SqlHelper.TemplateList, function (i, val1) {
                        if (val1.versionId == val.versionId) {
                            debugger;
                            var liVerson = $("<li id='" + val1.id + "' title='" + val1.templateName + "[" + val1.versionNum + "]" + "'>" + val1.templateName + "[" + val1.versionNum + "]" + "</li>");
                            $("#ulVersion").append(liVerson);

                            liVerson.bind({
                                "click": function () {
                                    openTemplateId = val1.id;
                                    $("#ulVersion").find("li").css("background-color", "white");
                                    liVerson.css("background-color", "#26a0da");
                                },
                                "dblclick": function () {
                                    openTemplateId = val1.id;
                                    $("#btnOpenOK").click();
                                }
                            });

                        }
                    })
                }
            })

        }
    });
}

function zTreeOnDblClick() {
    $("#btnCreateOK").click();
}



